package com.eorchis.module.planopinion.service.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import com.eorchis.core.annotation.AuditModule;
import com.eorchis.core.basedao.dao.IDaoSupport;
import com.eorchis.core.basedao.entity.IBaseEntity;
import com.eorchis.core.service.impl.AbstractBaseService;
import com.eorchis.module.planopinion.bean.PlanOpinionExportBean;
import com.eorchis.module.planopinion.dao.IPlanOpinionDao;
import com.eorchis.module.planopinion.domain.PlanOpinion;
import com.eorchis.module.planopinion.service.IPlanOpinionService;
import com.eorchis.module.planopinion.ui.commond.PlanOpinionQueryCommond;
import com.eorchis.module.planopinion.ui.commond.PlanOpinionValidCommond;
import com.eorchis.utils.utils.PropertyUtil;

/**
 * Title: PlanOpinionServiceImpl<br>
 * Description: 填写计划意见实体表管理业务接口实现<br>
 * Company: GoldGov<br>
 * Copyright @ 2012 GoldGov .All rights reserved.<br>
 * @author RongSL
 * @createDate 2016-07-12
 * @version $Revision$
 */
// 声明本类为Server实现类，声明bean名称
@Service(value = "main.java.com.eorchis.module.planopinion.service.impl.PlanOpinionServiceImpl")
@AuditModule("填写计划意见实体表")
public class PlanOpinionServiceImpl 
			extends AbstractBaseService 
		implements IPlanOpinionService {
		
	@Autowired
	@Qualifier("main.java.com.eorchis.module.planopinion.dao.impl.PlanOpinionDaoImpl")
	private IPlanOpinionDao planOpinionDao;
		

	public IDaoSupport getDaoSupport() {
		return planOpinionDao;
	}
	@SuppressWarnings("unchecked")
	public PlanOpinionValidCommond toCommond(IBaseEntity entity) {
		return new PlanOpinionValidCommond((PlanOpinion)entity);
	}


	/**
	 * 查找下发表ID
	 * @author RongSL
	 * @Date 2016-7-21
	 * */
	public String findIssuedId(PlanOpinionQueryCommond queryCommond){
		return planOpinionDao.findIssuedId(queryCommond);
	}
	
	/**
	 * 意见汇总数据
	 * @author RongSL
	 * @Date 2016-07-21
	 * */
	public List<PlanOpinionExportBean> opinionSummary(PlanOpinionQueryCommond queryCommond){
		return planOpinionDao.opinionSummary(queryCommond);
	}
	
	/**
	 * 意见反馈详情(单个)
	 * @author RongSL
	 * @Date 2016-07-25
	 * */
	public PlanOpinionExportBean Detailed(PlanOpinionExportBean bean){
		PlanOpinionQueryCommond queryCommond=new PlanOpinionQueryCommond();
		BeanUtils.copyProperties(bean, queryCommond);
		queryCommond.setSearchPlanId(bean.getPlanId());
		List<PlanOpinionExportBean> list=planOpinionDao.opinionDetailed(queryCommond);
		if(PropertyUtil.objectNotEmpty(list)){
			bean=list.get(0);
			List<?> opinionType=planOpinionDao.getOpinionType();
			Map<String,String> map=getMap(opinionType);
			String opinionTitles[]=new String[list.size()];
			String opinionValues[]=new String[list.size()];
			int index=0;
			for(String m:map.keySet()){
				for(PlanOpinionExportBean p:list){
					if(m.equals(p.getOpinionTitle())){
						opinionTitles[index]=map.get(m);
						opinionValues[index]=p.getOpinionValue();
						index++;
						break;
					}
				}
			}
			
			bean.setOpinionTitles(opinionTitles);
			bean.setOpinionValues(opinionValues);
		}
		return bean;
	}
	
	/**
	 * 意见明细数据
	 * @author RongSL
	 * @Date 2016-07-21
	 * */
	public Map<String,List<PlanOpinionExportBean>> opinionDetailed(PlanOpinionQueryCommond queryCommond){
		List<PlanOpinionExportBean> list1=planOpinionDao.opinionDetailed(queryCommond);
		List<PlanOpinionExportBean> list2=new ArrayList<PlanOpinionExportBean>();
		List<PlanOpinionExportBean> poebList;//最终集合
		List<?> opinionType=planOpinionDao.getOpinionType();
		String type[] = null;
		Map<String,List<PlanOpinionExportBean>> map1=encapsulationOne(list1);//得到封装成单个班级的集合
		Map<String,List<PlanOpinionExportBean>> map2;
		Map<String,List<PlanOpinionExportBean>> map3;
		for(String s:map1.keySet()){
			poebList=new ArrayList<PlanOpinionExportBean>();
			list2=map1.get(s);
			map2=encapsulationTwo(list2);//得到封装成各个时间节点的班级集合
			for(String st:map2.keySet()){
				list2=map2.get(st);
				map3=encapsulationThree(list2);//得到封装成各个部门的班级集合
				for(String str:map3.keySet()){
					list2=map3.get(str);
					type=new String[opinionType.size()];
					for(int i=0;i<opinionType.size();i++){
						for(PlanOpinionExportBean poe:list2){
							if(PropertyUtil.objectNotEmpty(poe.getOpinionTitle())&&poe.getOpinionTitle().equals(((Object[])opinionType.get(i))[0])){
								type[i]=poe.getOpinionValue();
								break;
							}
							type[i]="无";
						}
					}
					PlanOpinionExportBean poeb=list2.get(0);
					poeb.setOpinionTitle(null);
					poeb.setOpinionValue(null);
					poeb.setOpinionValues(type);
					poebList.add(poeb);
				}
			}
			map1.put(s, poebList);
		}
		return map1;
	}
	
	/**
	 * 一次封装，按班封装
	 * */
	public Map<String,List<PlanOpinionExportBean>> encapsulationOne(List<PlanOpinionExportBean> list){
		List<PlanOpinionExportBean> list2=null;
		Iterator<PlanOpinionExportBean> ite=list.iterator();
		Map<String,List<PlanOpinionExportBean>> pid=new LinkedHashMap<String,List<PlanOpinionExportBean>>();
		while(ite.hasNext()){
			PlanOpinionExportBean po=ite.next();
			if(pid.containsKey(po.getPlanId())){
				list2= pid.get(po.getPlanId());
				list2.add(po);
				pid.put(po.getPlanId(),list2);
			}else{
				list2=new ArrayList<PlanOpinionExportBean>();
				list2.add(po);
				pid.put(po.getPlanId(),list2 );
			}
		}
		return pid;
	}
	/**
	 * 二次封装，按时间封装
	 * */
	public Map<String,List<PlanOpinionExportBean>> encapsulationTwo(List<PlanOpinionExportBean> list){
		List<PlanOpinionExportBean> list2=null;
		Iterator<PlanOpinionExportBean> ite=list.iterator();
		Map<String,List<PlanOpinionExportBean>> pid=new LinkedHashMap<String,List<PlanOpinionExportBean>>();
		while(ite.hasNext()){
			PlanOpinionExportBean po=ite.next();
			if(pid.containsKey(po.getFeedbackDateStr())){
				list2= pid.get(po.getFeedbackDateStr());
				list2.add(po);
				pid.put(po.getFeedbackDateStr(),list2);
			}else{
				list2=new ArrayList<PlanOpinionExportBean>();
				list2.add(po);
				pid.put(po.getFeedbackDateStr(),list2 );
			}
		}
		return pid;
	}
	/**
	 * 三次封装，按部门封装
	 * */
	public Map<String,List<PlanOpinionExportBean>> encapsulationThree(List<PlanOpinionExportBean> list){
		List<PlanOpinionExportBean> list2=null;
		Iterator<PlanOpinionExportBean> ite=list.iterator();
		Map<String,List<PlanOpinionExportBean>> pid=new LinkedHashMap<String,List<PlanOpinionExportBean>>();
		while(ite.hasNext()){
			PlanOpinionExportBean po=ite.next();
			if(pid.containsKey(po.getDepName())){
				list2= pid.get(po.getDepName());
				list2.add(po);
				pid.put(po.getDepName(),list2);
			}else{
				list2=new ArrayList<PlanOpinionExportBean>();
				list2.add(po);
				pid.put(po.getDepName(),list2 );
			}
		}
		return pid;
	}
	
	/**
	 * 获得意见分类
	 * @author RongSL
	 * @Date 2016-07-22
	 * */
	public List<?> getOpinionType(){
		return planOpinionDao.getOpinionType();
	}
	
	public Map<String,String> getMap(List<?> list){
		Map<String,String> map=new LinkedHashMap<String,String>();
		Iterator<?> ite=list.iterator();
		while(ite.hasNext()){
			Object[] o=(Object[]) ite.next();
			map.put(o[0].toString(), o[1].toString());
		}
		return map;
	}
	
	/**
	 * 更新下发状态
	 * @author RongSL
	 * @Date 2016-10-11
	 * */
	public void updatePlanIssuedState(String iid){
		planOpinionDao.updatePlanIssuedState(iid);
	}
}
